
package models

import "errors"

type SystemUserRole struct {
	Id           int `json:"id" xorm:"not null pk autoincr comment('主键') INT(11)"`
	UserId int `json:"system_user_id" xorm:"not null comment('用户主键') INT(11)"`
	RoleId int `json:"system_role_id" xorm:"not null comment('角色主键')  INT(11)"`
}
var systemuserrole ="system_user_role"

func(u *SystemUserRole) GetRow() bool {
	has, err := mEngine.Get(u)
	if err==nil &&  has  {
		return true
	}
	return false
}
/*
func(u *SystemUserRole) GetRowByUid() ([]string,error) {
	var role []string
	err := mEngine.Table(systemuserrole).Select(systemrole+".rolename").
		Join("INNER", systemrole, systemuserrole+".roleid = "+systemrole+".id").
		Where(systemuserrole+".userid = ?", u.UserId).
		Find(&role)
	return role,err
}*/

func(u *SystemUserRole) GetRoleIdByUid() (int,error) {
	var roleIds []int
	err := mEngine.Table(systemuserrole).Select("role_id").
		Where("user_id = ?", u.UserId).
		Find(&roleIds)
	if len(roleIds)==0{
		return 0,errors.New("no corresponding role")
	}
	return roleIds[0],err
}

